﻿app.filter('startFrom', function () {
    return function (input, start) {
        if (input) {
            start = +start; //parse to int
            return input.slice(start);
        }
        return [];
    }
});
app.controller('BannerController', ['$scope', 'GlobalService','fileUpload', 'ApplicationService', 'alertboxservice', function ($scope, GlobalService,fileUpload, ApplicationService, alertboxservice) {


    BindGrid();

    $scope.toggle = function (modelstate, id) {

        $scope.modelstate = modelstate;
        $scope.id = id;
        switch (modelstate) {
            case 'add':
                $scope.title = "Add Banner";
                $scope.banner = null;
                break;
            case 'edit':
                $scope.title = "Edit Banner";
                url = "Banner/GetBanner";
                ApplicationService.Getdata(url, id).success(function (data) {
                    $scope.banner = data;
                });
                break;
        }
        $("#Banner").modal({ backdrop: 'static' });
    }

    function BindGrid() {
        url = "Banner/GetBanners";
        ApplicationService.Getdatas(url).success(function (data) {
            $scope.list = data;
            $scope.currentPage = 1;
            $scope.entryLimit = 10;
            $scope.filteredItems = $scope.list.length;
            $scope.totalItems = $scope.list.length;
        });
    }

    $scope.setPage = function (pageNo) {
        $scope.currentPage = pageNo;
    };

    $scope.sort_by = function (predicate) {
        $scope.predicate = predicate;
        $scope.reverse = !$scope.reverse;
    };

    $scope.delete = function (data, dataid) {
        swal({
            title: "Are you sure?",
            text: "Your will not be able to recover " + data + " Details!",
            type: "warning",
            showCancelButton: true,
            cancelButtonClass: 'btn-outline btn-black mr-10',
            cancelButtonText: 'Cancel',
            confirmButtonClass: "btn-danger",
            confirmButtonText: "Delete",
            closeOnConfirm: false
        }, function (status) {
            if (status) {
                url = "Banner/DeleteBanner";
                ApplicationService.DeleteData(url, dataid, data).success(function (data) {
                    if (data == "DSUS") {
                        swal("Deleted!", "Details deleted successfully", "success");
                    }
                    else {
                        swal("failed", "Unable to delete this details", "error");
                    }
                    BindGrid();
                });
            }
        });
    }

    $scope.save = function (modelstate, id) {
        var file = $scope.Image;
        $scope.buttonstate = true;
        var url;
        switch (modelstate) {
            case 'add':
                url = "Banner/AddBanner";
                break;
            case 'edit':
                url = "Banner/UpdateBanner";
                break;
        }
        var uploadUrl = "/CommonDataService/DataService/UploadBannerImage";
        if (angular.isUndefined(file) == false) {
            file = $scope.Image;
            fileUpload.uploadFileToUrl(file, uploadUrl).then(function (data) {
                $scope.banner.BannerImage = data.data;
                ApplicationService.Save($scope.banner, url).success(function (result) {
                    alertboxservice.ShowMsg(result, "Banner ");
                    $scope.buttonstate = false;
                    BindGrid();
                });
            });
        }
        else {
            ApplicationService.Save($scope.banner, url).success(function (result) {
                alertboxservice.ShowMsg(result, "Banner ");
                $scope.buttonstate = false;
                BindGrid();
            });
        }
    }
}]);

